React 开发简书项目实战总结


问题和解决方案

1.create-react-app 构建的项目释放配置文件 webpack 等时,运行 npm run eject 报错

  • 解决方案:使用 git 提交一次记录
1
2
3
git add .
git commit -m 'init'
npm run eject

2.styled-components 组件升级v4版本的全局样式踩坑,直接像原 injectGlobal 方法一样使用不能生效

  • 解决方案:定义一个全局样式变量,这个变量将作为组件插入文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//style.js,全局样式
import {createGlobalStyle} from 'styled-components';
export const GlobalStyle=createGlobalStyle`
...
`
//App.js,全局引入
import React, { Component,Fragment } from 'react';
import Header from './common/header';
import { GlobalStyle } from './style';
import { GlobalIconFont } from './statics/iconfont/iconfont';
class App extends Component {
render() {
return (
<Fragment>
<GlobalStyle />
<GlobalIconFont />
<Header />
</Fragment>
);
}
}